/*
* $Id: FormHelper.java 272 2011-02-21 05:37:35Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2010 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.common;

import java.util.Vector;

import apiary.Apiary;
import apiary.db.FamilyStore.FamilyRecord;
import apiary.db.HousingStore.HousingRecord;
import apiary.db.PointStore.PointRecord;
import apiary.db.QueenBeeStore.QueenBeeRecord;
import apiary.db.managers.DBManager;
import apiary.db.managers.InspectManager.InspectManagerRecord;
import apiary.ui.CategoryItem;
import apiary.ui.ItemEventListener;
import apiary.ui.journal.ChooseList;
import apiary.ui.journal.ChooseForm;
import apiary.ui.journal.ChooseForm.ChooseValue;

public class FormHelper
{
    public static void fillInspectList(ChooseForm form, int idFamily) {
        Vector inspects = DBManager.getInstance().getInspects(idFamily);
        for(int i = 0; i < inspects.size(); i++) {
            InspectManagerRecord inspect = (InspectManagerRecord) inspects.elementAt(i);
            CategoryItem item = FormHelper.createCategoryItem(form,
                String.valueOf(inspect.journal.idJournal),
                ConvertFunction.dateToString(inspect.journal.date.getTime()), "");
            form.append(item);
        }
    }
    
    public static void fillHousingList(ChooseForm form, int idFamily) {
        Vector chooseData = new Vector();
        Apiary apiary = Apiary.getInstance();
        DBManager dBManager = DBManager.getInstance();
        Vector housings = dBManager.getHousings(idFamily);
        for(int i = 0; i < housings.size(); i++) {
            HousingRecord record = (HousingRecord) housings.elementAt(i);
            ChooseValue value = form.new ChooseValue();
            value.id = record.idHousing;
            value.value = record.name;
            chooseData.addElement(value);
        }
        form.setData(chooseData);
    }

    public static void fillQuennBeeList(ChooseForm form, int idFamily) {
        Vector chooseData = new Vector();
        Apiary apiary = Apiary.getInstance();
        DBManager dBManager = DBManager.getInstance();
        Vector housings = dBManager.getQueenBees(idFamily);
        for(int i = 0; i < housings.size(); i++) {
            QueenBeeRecord record = (QueenBeeRecord) housings.elementAt(i);
            ChooseValue value = form.new ChooseValue();
            value.id = record.idQueenBee;
            value.value = record.nameQueenBee;
            chooseData.addElement(value);
        }
        form.setData(chooseData);
    }

    public static void fillPointList(ChooseForm form) {
        Vector chooseData = new Vector();
        Vector points = DBManager.getInstance().getPoints();
        for(int i = 0; i < points.size(); i++) {
            ChooseValue value = form.new ChooseValue();
            PointRecord point = (PointRecord) points.elementAt(i);
            value.value = point.namePoint;
            value.id = point.idPoint;
            chooseData.addElement(value);
        }
        form.setData(chooseData);
    }

    public static void fillChooseQBHList(ChooseForm form, String one, String two) {
        Vector chooseData = new Vector();
        Apiary apiary = Apiary.getInstance();
        ChooseValue value = form.new ChooseValue();
        value.value = one;
        value.id = 0;
        chooseData.addElement(value);
        value = form.new ChooseValue();
        value.value = two;
        value.id = 1;
        chooseData.addElement(value);
        form.setData(chooseData);
    }

    public static void fillFamilyList(int idPoint, ChooseForm form) {
        Vector chooseData = new Vector();
        Vector families = DBManager.getInstance().getFamilies(idPoint);
        for(int i = 0; i < families.size(); i++) {
            ChooseValue value = form.new ChooseValue();
            FamilyRecord family = (FamilyRecord) families.elementAt(i);
            value.value = family.name;
            value.id = family.idFamily;
            chooseData.addElement(value);
        }
        form.setData(chooseData);
    }

    public static void fillFamilyList(ChooseForm form) {
        DBManager dBManager = DBManager.getInstance();
        Vector points = dBManager.getPoints();
        for(int i = 0; i < points.size(); i++) {
            PointRecord point = (PointRecord) points.elementAt(i);
            Vector families = dBManager.getFamilies(point.idPoint);
            if(families.size() > 0)
                form.append(point.namePoint);
            for(int j = 0; j < families.size(); j++) {
                FamilyRecord family = (FamilyRecord) families.elementAt(j);
                String idFam = String.valueOf(family.idFamily);
                CategoryItem item = createCategoryItem(form, idFam, family.name, "");
                form.append(item);
            }
        }
        form.CheckEmpty();
    }

    public static void fillFamilyList(ChooseList list) {
        String nameEmpty = Apiary.getInstance().getLocaleString("NoFamily");
        CategoryItem item = createCategoryItem(list, "0", nameEmpty, "");
        list.append(item);
        DBManager dBManager = DBManager.getInstance();
        Vector points = dBManager.getPoints();
        for(int i = 0; i < points.size(); i++) {
            PointRecord point = (PointRecord) points.elementAt(i);
            Vector families = dBManager.getFamilies(point.idPoint);
            if(families.size() > 0)
                list.append(point.namePoint);
            for(int j = 0; j < families.size(); j++) {
                FamilyRecord family = (FamilyRecord) families.elementAt(j);
                String idFam = String.valueOf(family.idFamily);
                item = createCategoryItem(list, idFam, family.name, "");
                list.append(item);
            }
        }
    }

    public static CategoryItem createCategoryItem(ItemEventListener listener,
            String id, String name, String image) {
        CategoryItem item = new CategoryItem(id, name, image);
        item.setColor(255, 255, 255);
        item.setColorActive(221, 165, 54);
        item.setItemEventListener(listener);
        return item;
    }
}
